package com.lin.cloud.disk.manage.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.lin.cloud.disk.entity.pojo.DepartmentPermission;
import com.lin.cloud.disk.entity.pojo.Permission;
import com.lin.cloud.disk.entity.request.AuthorizationPermissionRequest;
import com.lin.cloud.disk.manage.IPermissionManageService;
import com.lin.cloud.disk.service.IPermissionService;
import com.lin.cloud.disk.service.IDepartmentPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @program: blog
 * @description: 权限表服务实现类
 * @author: Lin
 * @create: 2020-12-10 22:34
 **/
@Slf4j
@Service
public class PermissionManageServiceImpl implements IPermissionManageService {

    @Resource
    private IDepartmentPermissionService departmentPermissionService;

    @Resource
    private IPermissionService permissionService;

    /**
     * 获取权限列表通过部门ID列表
     * @param departmentIdList
     * @return
     */
    @Override
    public List<Permission> getPermissionByDepartmentIdList(List<Long> departmentIdList) {
        if (CollectionUtils.isEmpty(departmentIdList)){
            return Collections.emptyList();
        }
        List<Long> permissionIdList = departmentPermissionService.selectPermissionIdListByDepartmentIdList(departmentIdList);
            if (CollectionUtil.isNotEmpty(permissionIdList)){
            List<Permission> permissionsList = permissionService.selectPermissionListByPermissionIdList(permissionIdList);
            if (CollectionUtil.isNotEmpty(permissionsList)){
                return permissionsList;
            }
        }
        return Collections.emptyList();
    }



    /**
     * 授权
     * @param request
     */
    @Override
    public void authorizationPermission(AuthorizationPermissionRequest request) {
        List<Long> authorizationPartList = request.getAuthorizationPartList();
        List<Long> unAuthorizationPartList = request.getUnAuthorizationPartList();

        List<Long> coverAuthorizationPartList = new ArrayList<>(authorizationPartList.size());
        // 交集部分
        coverAuthorizationPartList.retainAll(unAuthorizationPartList);

        // 计算需要授权的部分
        authorizationPartList.removeAll(coverAuthorizationPartList);


        // 不算需要取消授权的部分
        unAuthorizationPartList.removeAll(coverAuthorizationPartList);

        DepartmentPermission departmentPermission =  null;
        // 批量添加授权
        if (CollectionUtil.isNotEmpty(authorizationPartList)){
            List<DepartmentPermission> departmentPermissionList = new ArrayList<>(authorizationPartList.size());
            for (Long permissionId : authorizationPartList) {
                departmentPermission = new DepartmentPermission();
                departmentPermission.setDepartmentId(request.getDepartmentId());
                departmentPermission.setCreateBy(request.getUserDTO().getUsername());
                departmentPermission.setCreateTime(new Date());
                departmentPermission.setPermissionId(permissionId);
                departmentPermission.setIsDelete(0);
                departmentPermissionList.add(departmentPermission);
            }
            // 添加到数据库
            departmentPermissionService.batchInsertDepartmentPermission(departmentPermissionList);
        }

        // 批量取消授权
        if (CollectionUtil.isNotEmpty(unAuthorizationPartList)){
            departmentPermissionService.batchDeleteDepartmentPermission(request.getDepartmentId(),unAuthorizationPartList);
        }

    }
}
